package com.huatek.springframework.view.extn;

import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class MyViewResolver implements ViewResolver{
	private static final Logger logger = Logger.getLogger(MyViewResolver.class);

	private ViewResolver tilesResolver;
    private ViewResolver jspResolver;
    private ViewResolver ajaxResolver;

    public void setAjaxResolver(ViewResolver ajaxResolver) {
		this.ajaxResolver = ajaxResolver;
	}

	public void setJspResolver(ViewResolver jspResolver) {
        this.jspResolver = jspResolver;
    }

    public void setTilesResolver(ViewResolver tilesResolver) {
        this.tilesResolver = tilesResolver;
    }

	public View resolveViewName(String viewName, Locale locale) throws Exception {
		if(isJspView(viewName)){
			if(logger.isDebugEnabled()){
				logger.debug("当前页面："+viewName);
			}
			return jspResolver.resolveViewName(viewName, locale);
		}
		if(isAjaxView(viewName)){
			return ajaxResolver.resolveViewName(viewName, locale);
		}
		return tilesResolver.resolveViewName(viewName, locale);
	}
	private boolean isJspView(String viewName){
		return viewName.endsWith(".jsp");
	}
	private boolean isAjaxView(String viewName){
		return viewName.startsWith("ajax_");
	}

}
